/*
Copyright (c) 2012 Yathindu Rangana
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
-----------------------------------------------------------------------------
Filename:    OgreFaceDemo.h
-----------------------------------------------------------------------------


This source file is generated by the
   ___                   _              __    __ _                  _ 
  /___\__ _ _ __ ___    /_\  _ __  _ __/ / /\ \ (_)______ _ _ __ __| |
 //  // _` | '__/ _ \  //_\\| '_ \| '_ \ \/  \/ / |_  / _` | '__/ _` |
/ \_// (_| | | |  __/ /  _  \ |_) | |_) \  /\  /| |/ / (_| | | | (_| |
\___/ \__, |_|  \___| \_/ \_/ .__/| .__/ \/  \/ |_/___\__,_|_|  \__,_|
      |___/                 |_|   |_|                                 
      Ogre 1.7.x Application Wizard for VC10 (July 2011)
      http://code.google.com/p/ogreappwizards/
-----------------------------------------------------------------------------
*/
#ifndef __OgreFaceDemo_h_
#define __OgreFaceDemo_h_

#include "stdafx.h"

#include "BaseApplication.h"
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#include "../res/resource.h"
#endif

#include "OgreFace.h"
#include "EventListener.h"
/**
	@mainpage OgreFace Documentation

	@section intro_sec Introduction
	<p>
	OgreFace in a small set of utility and wrapper classes around SeeingMachine's FaceAPI for use with Ogre 3D graphics library.
	<br>OgreFace aims to make it easier for developers using Ogre to add head tracking into their applications.
	</p>
	<p>
	OgreFace was built using Ogre 1.7.4 and FaceAPI Non-Commercial 3.2.
	</p>
*/
/**
	Notice that this class derives from EventSystem::EventListener to allow it self to be added to the OgreFace's list of listeners
	It also derives from the usual BaseApplication that all Ogre demos do.
*/
class OgreFaceDemo : public BaseApplication, public EventSystem::EventListener
{
public:
    OgreFaceDemo(void);
    virtual ~OgreFaceDemo(void);

	/**
		@brief This receives the events from any EventSystem
		@param	e	The event
		@param	source	The event manager that fired the event, in this  demo there is only one event manager (the one in OgreFace) but you can have other event managers doing other things. Their events will also be received by this function.
	*/
	void catchEvent(const EventSystem::EventPtr& e, EventSystem::EventManager& source);
protected:
	/**
		@brief	Display the help dialog
	*/
	void displayHelp();

	bool frameRenderingQueued(const Ogre::FrameEvent& evt);

	bool keyReleased( const OIS::KeyEvent &arg );

	/**
		@brief	Initializes OgreFace and sets's up the scene.
	*/
    virtual void createScene(void);
	/**
		@brief Sets the lights attenuation to be a exponencial falloff. More realistic light attenuation
		@param	L	The light to set range of
		@param	Range	The range of the light
	*/
	void setLightRange( Ogre::Light *L, Ogre::Real Range )
	{
		L->setAttenuation( Range, 1.0f, 4.5/Range, 75.0f/(Range*Range) );
	}

	Ogre::SceneNode* rayCollide(Ogre::Vector3 origin, Ogre::Vector3 direction)
	{
		
		//put the ray a bit behind the camera, so it can respond faster to a collision
		//Ogre::Ray cameraRay(m_pCameraNode->getPosition() , m_pCameraNode->convertLocalToWorldPosition(m_pActor->getBodyAsCritterBody()->getSceneNode()->getPosition())*(Ogre::Vector3::UNIT_Z * 5.0f));;
		Ogre::Ray cameraRay(origin, direction);
		m_pRaySceneQuery->setRay(cameraRay);

		Ogre::RaySceneQueryResult &result = m_pRaySceneQuery->execute();
		Ogre::RaySceneQueryResult::iterator itr = result.begin();

		if (itr != result.end() && (itr->movable || itr->worldFragment))
		{
			if(itr->movable->getName() != "building")
				return itr->movable->getParentSceneNode();
		}
	
	
		return 0;
	}

	Ogre::RaySceneQuery* m_pRaySceneQuery;
	const float RayDelay; //< We don't want to be casting a ray every frame!
	float m_fTimeSinceRay;
	Ogre::SceneNode* m_pCurrSelected;
	float m_fCurrElapsedTime;

	//demo vars
	Ogre::Quaternion m_oldQuat;
	Ogre::Vector3	m_oldYPR;
	Ogre::Light* l;

	OgreBites::ParamsPanel* m_pFaceDebugPanel;   // sample details panel

	bool m_bWorkAround;
};

#endif // #ifndef __OgreFaceDemo_h_
